package edzest.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;
import javax.swing.JCheckBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtBtn;
	private JPasswordField pwdBtn;
	private JList list1;
	private JList list2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle("기본 컴퍼넌트");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JLabel imgLabel = new JLabel("이미지레이블");
			imgLabel.setIcon(new ImageIcon(BComp.class.getResource("/img/icon2.png")));
			imgLabel.setFont(new Font("맑은 고딕", Font.PLAIN, 16));
			contentPanel.add(imgLabel);
		}
		{
			JButton imgBtn = new JButton("이미지 버튼");
			imgBtn.setToolTipText("이미지버튼입니다");
			imgBtn.setFont(new Font("맑은 고딕", Font.PLAIN, 14));
			imgBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			imgBtn.setIcon(new ImageIcon(BComp.class.getResource("/icon/icon.png")));
			contentPanel.add(imgBtn);
		}
		{
			JCheckBox chkSuji = new JCheckBox("수지");
			chkSuji.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					/*int selected = 1;
					if ( e.getStateChange() ==  ItemEvent.SELECTED )
						// 이벤트(e)가 발생하면 getStateChange()가 상태를 감지하고 SELECTED면 아래를 실행
						selected = 1;  // 선택됨
					else 
						selected = -1; // 비선택
					
					if(selected == 1)
						// JOptionPane : 팝업 다이얼로그창
						JOptionPane.showMessageDialog(null, e.getItem()+" 을 선택!");*/
					
					JCheckBox c = (JCheckBox) e.getSource();
					if ( c.isSelected() )
						// JOptionPane.showMessageDialog(null, "수지 선택!"); -> 팝업은 뜨는데 체크박스에 체크가 안됨
						txtBtn.setText( c.getText() );
					else 
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(chkSuji);
		}
		{
			JCheckBox chkHye = new JCheckBox("혜교");
			contentPanel.add(chkHye);
		}
		{
			JCheckBox chkHyun = new JCheckBox("지현");
			contentPanel.add(chkHyun);
		}
		
		// 라디오 버튼 선언 시작
			JRadioButton radSuji = new JRadioButton("수지");
			radSuji.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton)e.getSource();
					// getSource() -> Object java.util.EventObject.getSource()
					
					if ( r.isSelected() ) // 라디오버튼(r)이 선택되면 라디오버튼의 텍스트를 텍스트필드에 입력하고
						// JOptionPane.showMessageDialog(null, "수지가 선택되었어요!!");
						txtBtn.setText( r.getText() );
					else // 선택되지 않으면 텍스트필드의 내용을 빈칸으로 한다
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(radSuji);
		
			JRadioButton radHye = new JRadioButton("혜교");
			contentPanel.add(radHye);
		
			JRadioButton radHyun = new JRadioButton("지현");
			contentPanel.add(radHyun);
		// 라디오 버튼 선언 끝
			
		// 라디오 버튼 그룹 생성 및 추가
		ButtonGroup rgroup = new ButtonGroup();
		rgroup.add(radSuji);
		rgroup.add(radHye);
		rgroup.add(radHyun);
		{
			txtBtn = new JTextField();
			contentPanel.add(txtBtn);
			txtBtn.setColumns(10);
		}
		{
			JButton txt1Btn = new JButton("입력문자보기");
			txt1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog( null, txtBtn.getText() );
					// textField에 입력된 문자 출력
				}
			});
			contentPanel.add(txt1Btn);
		}
		{
			pwdBtn = new JPasswordField();
			pwdBtn.setEchoChar('♥');
			pwdBtn.setColumns(16);
			contentPanel.add(pwdBtn);
		}
		{
			JButton pwd1Btn = new JButton("입력문자보기");
			pwd1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// JOptionPane.showMessageDialog( null, pwdBtn.getText() );
					// 조만간 없어질 메서드이므로 사용하지마라
					JOptionPane.showMessageDialog( null, pwdBtn.getPassword() );
				}
			});
			contentPanel.add(pwd1Btn);
		}
		{
			JScrollPane scrollPane1 = new JScrollPane();  // 항목이 길면 자동으로 스크롤바를 생성해줌
			scrollPane1.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane1);
			{
				// JList에 추가할 항목을 배열로 생성
				String[] data = {"수지","혜교","지현","수지","혜교","지현","수지","혜교","지현","수지","혜교","지현"};
				// 생성된 배열을 JList의 생성자에 주입
				list1 = new JList( data ); // 밑에 listBtn이 인식 못해서 필드로 바꿈
				scrollPane1.setViewportView(list1);
			}
		}
		{
			JScrollPane scrollPane2 = new JScrollPane();
			scrollPane2.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane2);
			{
				list2 = new JList(); // 밑에 listBtn이 인식 못해서 필드로 바꿈
				
				list2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if (e.getClickCount() == 2){
							JOptionPane.showMessageDialog(null, list2.getSelectedIndex() + "번 째 " + list2.getSelectedValue());
						}
					}
				});
				
				list2.setModel(new AbstractListModel() {
					String[] values = new String[] {"혜교", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지"};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				scrollPane2.setViewportView(list2);
			}
		}
		{
			JButton listBtn = new JButton("리스트선택보기");
			listBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog( null, list1.getSelectedIndex() + "/" + list1.getSelectedValue() );
				}
			});
			contentPanel.add(listBtn);
		}
		{
			JScrollPane scrollPane3 = new JScrollPane();
			scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane3.setPreferredSize(new Dimension(250, 200));
			contentPanel.add(scrollPane3);
			{
				JTextArea textArea = new JTextArea();
				scrollPane3.setViewportView(textArea);
			}
		}
		{
			String[] data = {" 수지 "," 혜교 "," 지현 "," 민준 "," 정민 "," 수지 "," 혜교 "," 지현 "," 민준 "," 정민 "};
			final JComboBox comboBox1 = new JComboBox(data);
			comboBox1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JOptionPane.showMessageDialog(null, comboBox1.getSelectedItem() + "/" + comboBox1.getSelectedIndex());
				}
			});
			contentPanel.add(comboBox1);
		}
		{
			JComboBox comboBox2 = new JComboBox();
			comboBox2.setModel(new DefaultComboBoxModel(new String[] {"수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지", "수지"}));
			contentPanel.add(comboBox2);
		}
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("OK");
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
	}

}
